<?php namespace system\model;
use houdunwang\model\Model;
class Link extends Model{
	//数据表
	protected $table = "link";

	//允许填充字段
	protected $allowFill = [ '*'];

	//禁止填充字段
	protected $denyFill = [ ];

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
          ['lname','isnull','友链名称不能为空',self::MUST_VALIDATE,self::MODEL_BOTH],
          ['logo','isnull','友链logo不能为空',self::MUST_VALIDATE,self::MODEL_BOTH],
          ['url','isnull','友链地址不正确',self::MUST_VALIDATE,self::MODEL_BOTH],
          ['sort','isnull','友链排序不能为空',self::MUST_VALIDATE,self::MODEL_BOTH],
      ];

	//自动完成
	protected $auto=[
          //['字段名','处理方法','方法类型',验证条件,验证时机]
          ['addtime','time','function',self::MUST_AUTO,self::MODEL_INSERT],
	];

    /**
     * @return bool添加友链
     */
    public function store(){
        $this->lname = $_POST['lname'];
        $this->logo = $_POST['logo'];
        $this->url = $_POST['sort'];
        $this->sort = $_POST['sort'];
        //save()自动触发自动验证
        return $this->save();
    }

    //获取所有信息
    public function getAllLink($num){
        $count = Db::table('link')->count();
        //p($count);
        Page::row($num)->make($count);//每行显示有链内容条数
        $page = Page::desc(['pre'=>'< < 上一个','next'=>'下一个 > >','first'=>'首页','end'=>'尾页','unit'=>'个'])->make($count,$num);
        $data = Db::table('link')->limit(Page::limit())->get();
        return ['page'=>$page,'data'=>$data];
    }

    /**
     * 编辑
     */
    //获取旧数据
    public function getOldLink($lid){
        $editModel = $this::where('lid',$lid)->first()->toArray();
        //p($editModel);
        return $editModel;
    }
    public function edit(){
        $editModel = $this->find($_POST['lid']);
        $editModel->lname = $_POST['lname'];
        $editModel->logo = $_POST['logo'];
        $editModel->url = $_POST['sort'];
        $editModel->sort = $_POST['sort'];
        return $editModel->save();
    }
    public function delLink($lid){
        Db::table('link')->where('lid',$lid)->delete();
        return true;
        //或
        //Db::table('link')->delete($lid);
        //或
        //$this::delete($lid);
        //或
        //$this::where('lid',$lid)->delete();
    }
}